package com.service.admin.impl;

import com.dao.ReportDao;
import com.entity.CommentReport;
import com.entity.GoodReport;
import com.entity.Notice;
import com.service.admin.AdminReportService;
import com.service.user.NoticeService;
import com.utils.FileUploadUtil;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 管理员举报处理的实现类
 *
 * 
 */
@Service
public class AdminReportServiceImpl implements AdminReportService {
  @Autowired NoticeService noticeService;

  @Override
  public Map<String, Integer> getAllGoodReportCount() {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    Map<String, Integer> count = dao.selectAllGoodReportCount();
    session.close();
    return count;
  }

  @Override
  public List<Map<String, Object>> getAllGoodReport(int pageSize, int pageIndex) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int start = pageIndex * pageSize;
    List<Map<String, Object>> allGoodReport = dao.selectAllGoodReport(start, pageSize);
    session.close();

    int dealResult;
    String replyInformer;
    String noticeSeller;
    for (Map<String, Object> item : allGoodReport) {
      dealResult = (int) item.get("dealResult");
      replyInformer = (String) item.get("replyInformer");
      noticeSeller = (String) item.get("noticeSeller");
      if (dealResult == ReportDao.DISAGREE_GOOD_REPORT_RESULT) {
        if (replyInformer == null) {
          item.put("replyInformer", Notice.getDefaultTemplate(Notice.DISAGREE_GOOD_REPORT));
        }
      } else if (dealResult != ReportDao.UNTREATED) {
        // 同意举报
        if (replyInformer == null) {
          item.put("replyInformer", Notice.getDefaultTemplate(Notice.AGREE_GOOD_REPORT));
        }
        if (noticeSeller == null) {
          item.put("noticeSeller", Notice.getDefaultTemplate(dealResult + 3));
        }
      }
    }
    return allGoodReport;
  }

  @Override
  public Map<String, Integer> getAllCommentReportCount() {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    Map<String, Integer> count = dao.selectAllCommentReportCount();
    session.close();
    return count;
  }

  @Override
  public List<Map<String, Object>> getAllCommentReport(int pageSize, int pageIndex) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int start = pageIndex * pageSize;
    List<Map<String, Object>> allCommentReport = dao.selectAllCommentReport(start, pageSize);
    session.close();

    int dealResult;
    String replyInformer;
    String noticeCommentUser;
    for (Map<String, Object> item : allCommentReport) {
      dealResult = (int) item.get("dealResult");
      replyInformer = (String) item.get("replyInformer");
      noticeCommentUser = (String) item.get("noticeCommentUser");
      if (dealResult == ReportDao.DISAGREE_COMMENT_REPORT_RESULT) {
        if (replyInformer == null) {
          item.put("replyInformer", Notice.getDefaultTemplate(Notice.DISAGREE_COMMENT_REPORT));
        }
      } else if (dealResult != ReportDao.UNTREATED) {
        if (replyInformer == null) {
          item.put("replyInformer", Notice.getDefaultTemplate(Notice.AGREE_COMMENT_REPORT));
        }
        if (noticeCommentUser == null) {
          item.put("noticeCommentUser", Notice.getDefaultTemplate(dealResult + 13));
        }
      }
    }
    return allCommentReport;
  }

  @Override
  public int getManagerPutOffGoodCount() {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int count = dao.selectManagerPutOffGoodCount();
    session.close();
    return count;
  }

  @Override
  public List<Map<String, Object>> getManagerPutOffGood(int pageSize, int pageIndex) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int start = pageIndex * pageSize;
    List<Map<String, Object>> managerPutOffGood = dao.selectManagerPutOffGood(start, pageSize);
    session.close();
    int dealResult;
    String noticeSeller;
    for (Map<String, Object> item : managerPutOffGood) {
      dealResult = (int) item.get("dealResult");
      noticeSeller = (String) item.get("noticeSeller");
      if (dealResult != ReportDao.UNTREATED && noticeSeller == null) {
        item.put("noticeSeller", Notice.getDefaultTemplate(dealResult + 3));
      }
    }
    return managerPutOffGood;
  }

  @Override
  public int getManagerPutOffCommentCount() {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int count = dao.selectManagerPutOffCommentCount();
    session.close();
    return count;
  }

  @Override
  public List<Map<String, Object>> getManagerPutOffComment(int pageSize, int pageIndex) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int start = pageIndex * pageSize;
    List<Map<String, Object>> managerPutOffComment =
        dao.selectManagerPutOffComment(start, pageSize);
    session.close();
    int dealResult;
    String noticeCommentUser;
    for (Map<String, Object> item : managerPutOffComment) {
      dealResult = (int) item.get("dealResult");
      noticeCommentUser = (String) item.get("noticeCommentUser");
      if (dealResult != ReportDao.UNTREATED && noticeCommentUser == null) {
        item.put("noticeCommentUser", Notice.getDefaultTemplate(dealResult + 13));
      }
    }
    return managerPutOffComment;
  }

  @Override
  public int dealGoodReport(
      String adminName, String reportId, int result, String replyInformer, String noticeSeller) {
    if (result == ReportDao.DISAGREE_GOOD_REPORT_RESULT) {
      if (replyInformer.equals(Notice.getDefaultTemplate(Notice.DISAGREE_GOOD_REPORT))) {
        replyInformer = null;
      }
    } else if (result >= 1 && result <= 3) {
      if (replyInformer.equals(Notice.getDefaultTemplate(Notice.AGREE_GOOD_REPORT))) {
        replyInformer = null;
      }
      if (noticeSeller.equals(Notice.getDefaultTemplate(3 + result))) {
        noticeSeller = null;
      }
    } else {
      return 0;
    }
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int row = dao.updateGoodReportResult(adminName, reportId, result, replyInformer, noticeSeller);
    session.commit();

    dao.dealUnqualifiedProduct(reportId, result);
    session.commit();
    GoodReport report = dao.selectGoodReportById(reportId);
    session.close();

    // 发送通知
    String goodId = report.getGoodId();
    String goodName = report.getGoodName();
    String type = report.getType();
    String reason = report.getReason();
    String picURL1 = report.getPic1URL();
    String picURL2 = report.getPic2URL();
    String picURL3 = report.getPic3URL();
    Date reportTime = report.getReportTime();
    if (result == ReportDao.DISAGREE_GOOD_REPORT_RESULT) {
      noticeService.addNotice(
          report.getReportUserId(),
          Notice.DISAGREE_GOOD_REPORT,
          replyInformer,
          0,
          goodId,
          goodName,
          type,
          reason,
          picURL1,
          picURL2,
          picURL3,
          reportTime);
    } else {
      noticeService.addNotice(
          report.getReportUserId(),
          Notice.AGREE_GOOD_REPORT,
          replyInformer,
          0,
          goodId,
          goodName,
          type,
          reason,
          picURL1,
          picURL2,
          picURL3,
          reportTime);
      noticeService.addNotice(
          report.getSellerId(),
          3 + result,
          noticeSeller,
          0,
          goodId,
          goodName,
          type,
          reason,
          picURL1,
          picURL2,
          picURL3);
    }
    return row;
  }

  @Override
  public int setGoodUnqualified(
      String adminName,
      String goodId,
      String type,
      String reason,
      MultipartFile pic1,
      MultipartFile pic2,
      MultipartFile pic3,
      int result,
      String noticeSeller) {
    if (result >= 1 && result <= 3) {
      if (noticeSeller.equals(Notice.getDefaultTemplate(3 + result))) {
        noticeSeller = null;
      }
    } else {
      return 0;
    }
    // 举报编号
    String reportId = UUID.randomUUID().toString().replace("-", "");
    // 举报证据截图
    String pic1URL =
        FileUploadUtil.uploadFile(
            pic1, reportId + "1", FileUploadUtil.GOOD_REPORT_SUBDIR, FileUploadUtil.IMAGE_SUFFIX);
    String pic2URL =
        FileUploadUtil.uploadFile(
            pic2, reportId + "2", FileUploadUtil.GOOD_REPORT_SUBDIR, FileUploadUtil.IMAGE_SUFFIX);
    String pic3URL =
        FileUploadUtil.uploadFile(
            pic3, reportId + "3", FileUploadUtil.GOOD_REPORT_SUBDIR, FileUploadUtil.IMAGE_SUFFIX);

    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int row =
        dao.insertAUnqualifiedGoodRecord(
            reportId,
            goodId,
            type,
            reason,
            pic1URL,
            pic2URL,
            pic3URL,
            adminName,
            result,
            noticeSeller);
    session.commit();

    dao.dealUnqualifiedProduct(reportId, result);
    session.commit();

    GoodReport report = dao.selectGoodReportById(reportId);
    session.close();

    // 添加通知
    noticeService.addNotice(
        report.getSellerId(),
        3 + result,
        noticeSeller,
        0,
        goodId,
        report.getGoodName(),
        type,
        reason,
        report.getPic1URL(),
        report.getPic2URL(),
        report.getPic3URL());
    return row;
  }

  @Override
  public int dealCommentReport(
      String adminName,
      String reportId,
      int result,
      int banDays,
      String replyInformer,
      String noticeCommentUser) {

    if (result == ReportDao.DISAGREE_COMMENT_REPORT_RESULT) {
      if (replyInformer.equals(Notice.getDefaultTemplate(Notice.DISAGREE_COMMENT_REPORT))) {
        replyInformer = null;
      }
    } else if (result >= 1 && result <= 3) {
      if (replyInformer.equals(Notice.getDefaultTemplate(Notice.AGREE_COMMENT_REPORT))) {
        replyInformer = null;
      }
      if (noticeCommentUser.equals(Notice.getDefaultTemplate(13 + result))) {
        noticeCommentUser = null;
      }
    } else {
      return 0;
    }
    Date toTime = null;
    if (result == ReportDao.COMMENT_UNQUALIFIED_WARNING_RESULT) {
      toTime = dateAdder(banDays);
    }
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    // 更新举报记录
    int row =
        dao.updateCommentReportResult(
            adminName, reportId, result, banDays, replyInformer, noticeCommentUser);
    session.commit();
    // 对评论者和评论做出处理
    dao.dealUnqualifiedComment(reportId, toTime);
    session.commit();
    CommentReport report = dao.selectCommentReportById(reportId);
    session.close();

    // 添加通知
    String goodId = report.getGoodId();
    String goodName = report.getGoodName();
    String content = report.getCommentContent();
    String type = report.getType();
    String reason = report.getReason();
    Date reportTime = report.getReportTime();

    if (result == ReportDao.DISAGREE_COMMENT_REPORT_RESULT) {
      noticeService.addNotice(
          report.getReportUserId(),
          Notice.DISAGREE_COMMENT_REPORT,
          replyInformer,
          0,
          goodId,
          goodName,
          content,
          type,
          reason,
          reportTime);
    } else {
      noticeService.addNotice(
          report.getReportUserId(),
          Notice.AGREE_COMMENT_REPORT,
          replyInformer,
          0,
          goodId,
          goodName,
          content,
          type,
          reason,
          reportTime);
      noticeService.addNotice(
          report.getCommentUserId(),
          13 + result,
          noticeCommentUser,
          0,
          report.getCommentTime(),
          content,
          goodId,
          goodName,
          type,
          reason,
          toTime);
    }
    return row;
  }

  @Override
  public int setCommentUnqualified(
      String adminName,
      String commentId,
      String type,
      String reason,
      int result,
      int banDays,
      String noticeCommentUser) {
    if (result >= 1 && result <= 3) {
      if (noticeCommentUser.equals(Notice.getDefaultTemplate(13 + result))) {
        noticeCommentUser = null;
      }
    } else {
      return 0;
    }
    Date toTime = null;
    if (result == ReportDao.COMMENT_UNQUALIFIED_WARNING_RESULT) {
      toTime = dateAdder(banDays);
    }
    String rcId = UUID.randomUUID().toString().replace("-", "");
    SqlSession session = SqlSessionGetter.getSqlSession();
    ReportDao dao = session.getMapper(ReportDao.class);
    int row =
        dao.insertAUnqualifiedCommentRecord(
            rcId, commentId, type, reason, adminName, result, banDays, noticeCommentUser);
    session.commit();
    dao.dealUnqualifiedComment(rcId, toTime);
    session.commit();
    CommentReport report = dao.selectCommentReportById(rcId);
    session.close();

    // 添加通知
    noticeService.addNotice(
        report.getCommentUserId(),
        13 + result,
        noticeCommentUser,
        0,
        report.getCommentTime(),
        report.getCommentContent(),
        report.getGoodId(),
        report.getGoodName(),
        type,
        reason,
        toTime);
    return row;
  }

  private Date dateAdder(int banDays) {
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());
    calendar.add(Calendar.DAY_OF_MONTH, banDays);
    return calendar.getTime();
  }
}
